# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:

import requests
import os, time

class Tools:

    def __init__(self):
        self.cookies = {
            "JSESSIONID": "irzqoauiqvsb1pa65d26zdkg6"
        }

    def run(self):
        fileName = '/Users/duanmengdi/Desktop/changanImgOld.txt'
        basePath = '/Users/duanmengdi/Desktop/changanImg'
        with open(fileName, 'r') as f:
            data = f.read()
        cid_l = []
        for info in data.split('\n'):
            # print(info)
            if info == 'end...':
                break

            no_path = info.split(' ')[1]
            cid = info.split(' ')[2]
            cid_l.append(cid)
            # mid = info.split(' ')[3]
            # midPath = basePath + os.sep + no_path + os.sep + cid + os.sep + mid
            # if not os.path.exists(midPath):
            #     os.makedirs(midPath)
            # imgPath = midPath + os.sep + mid + '.png'
            # if os.path.exists(imgPath):
            #     print("continue")
            #     continue
            # time.sleep(3)
            # imgRes = self.getpdf(mid)
            # if imgRes:
            #     print("success")
            #     with open(imgPath, 'wb') as f:
            #         f.write(imgRes.content)
        print(list(set(cid_l)))
        print(len(set(cid_l)))

    def gethtml(self, mid):
        headers = {
            "Host": "epc.changan.com.cn:8080",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": "http://epc.changan.com.cn:8080/front/oldcar.html?type=1&scId=1431&scCode=SC6469GA5HEV.S52A",
        }
        url = 'http://epc.changan.com.cn:8080/front/pdfjs/web/viewer.html'
        params = '?{}={}'.format("file", "http://epc.changan.com.cn:8080/front/download/{}.pdf".format(mid))
        url = url + params
        response = requests.get(url=url, headers=headers, cookies=self.cookies, timeout=15)
        cookies = response.cookies.get_dict()
        for k in cookies:
            self.cookies[k]=cookies[k]

    def getpdf(self, mid):
        self.gethtml(mid)
        headers = {
            "Host": "epc.changan.com.cn:8080",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
        }
        headers["Referer"] = "http://epc.changan.com.cn:8080/front/pdfjs/web/viewer.html?file=http://epc.changan.com.cn:8080/front/download/{}.pdf".format(mid)
        url = 'http://epc.changan.com.cn:8080/front/download/{}.pdf'.format(mid)
        # url = 'http://epc.changan.com.cn:8080/front/download/60000-00-C201.pdf'
        response = requests.get(url=url, headers=headers, cookies=self.cookies, timeout=30)
        cookies = response.cookies.get_dict()
        for k in cookies:
            self.cookies[k] = cookies[k]
        if response:
            return response

if __name__ == '__main__':
    tools = Tools()
    tools.run()